package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.HealthException;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 包名：com.itheima.health.service.impl
 *
 * @author zy
 * @date 2020 -09-05 14:50
 */
@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    /**
     * 查询所有角色
     * @return
     */
    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult<Role> findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //    判断条件
        if(!StringUtils.isEmpty(queryPageBean.getQueryString())){
            queryPageBean.setQueryString("%"+queryPageBean.getQueryString()+"%");
        }
        Page<Role> page= roleDao.findByCondition(queryPageBean.getQueryString());
        return new PageResult<>(page.getTotal(),page.getResult());
    }

    /**
     * 添加角色
     * @param role
     * @param permissionIds
     * @param menuIds
     */
    @Override
    @Transactional
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //    第一步.添加角色表
        roleDao.add(role);
        //    第二步,添加角色和权限关系表
        Integer roleId = role.getId();
        if(null!=permissionIds){
            for (Integer permissionId : permissionIds) {
                roleDao.addRelationBetweenRoleAndPermission(roleId,permissionId);
            }
        }
        //    第三步,添加角色和菜单关系表
        if (null!=menuIds) {
            for (Integer menuId : menuIds) {
                roleDao.addRelationBetweenRoleAndMenu(roleId,menuId);
            }
        }

    }

    /**
     * 删除角色
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(int id) throws HealthException {
        //    判断用户有没有使用该角色
        Integer count = roleDao.findCount(id);
        if(count>0){
            throw new HealthException("该角色被用户使用,不能被删除");
        }
        //    先删除两个关联表
        roleDao.deleteRelationBetweenRoleAndPermission(id);
        roleDao.deleteRelationBetweenRoleAndMenu(id);
        //    最后删除该角色
        roleDao.deleteById(id);


    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @Override
    public Role findById(int id) {
        return roleDao.findById(id);
    }
    @Override
    public List<Integer> findMenuIdsByRoleId(int id) {
        return roleDao.findMenuIdsByRoleId(id);
    }
    @Override
    public List<Integer> findPermissionIdsByRoleId(int id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }

    @Override
    @Transactional
    public void update(Role role, Integer[] permissionIds, Integer[] menuIds) {
        Integer roleId = role.getId();
        //    更新角色表
        roleDao.update(role);
        //    删除角色与权限的旧关系
        roleDao.deleteRelationBetweenRoleAndPermission(roleId);
        //    添加角色与权限的新关系
        if(null!=permissionIds){
            for (Integer permissionId : permissionIds) {
                roleDao.addRelationBetweenRoleAndPermission(roleId,permissionId);
            }
        }
        //    删除角色与菜单的旧关系
        roleDao.deleteRelationBetweenRoleAndMenu(roleId);
        //    添加角色与菜单的新关系
        if (null!=menuIds) {
            for (Integer menuId : menuIds) {
                roleDao.addRelationBetweenRoleAndMenu(roleId,menuId);
            }
        }
    }
}
